package com.ebelter.myehc.adapters.historys;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.myehc.R;
import com.ebelter.myehc.models.http.response.GetBra;
import com.ebelter.myehc.ui.fragments.bracelet.BRA_SHM_Fragment;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ebelter.btcomlib.bases.fragment.BaseFragment.N2;


public class H_Bra_A extends BaseAdapter {

    private Context mContext;




    private List<GetBra.ResultDataBean.DataListBean> datas;

    public H_Bra_A(Context mContext) {
        this.mContext = mContext;
        datas=new ArrayList<>();
    }


    public void addData(List<GetBra.ResultDataBean.DataListBean> data){
        datas.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas!=null?datas.size():0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        VH vh;
        if (convertView==null){
            convertView=View.inflate(mContext, R.layout.item_sports_his,null);
            vh=new VH();
            vh.topLine=convertView.findViewById(R.id.item_sport_top_h_line);
            vh.buttomLine=convertView.findViewById(R.id.item_sport_buttom_h_line);


            vh.item_sp_date_tv=convertView.findViewById(R.id.item_xt_date_tv);

            vh.item_sp_bushu_tv=convertView.findViewById(R.id.item_sp_bushu_tv);
            vh.item_sp_distance_tv=convertView.findViewById(R.id.item_sp_lch_tv);
            vh.item_sp_carili_tv=convertView.findViewById(R.id.item_sp_kll_tv);
            vh.item_sp_xl_tv=convertView.findViewById(R.id.item_sp_xl_tv);
            vh.item_sp_shm_tv=convertView.findViewById(R.id.item_sp_shmshj_tv);
            convertView.setTag(vh);
        }else {
            vh= (VH) convertView.getTag();
        }


        GetBra.ResultDataBean.DataListBean data = datas.get(position);

        Date date = TimeUtils.parseFormatter1Time(datas.get(position).time);
       // String shiFen = TimeUtils.formatTime4(date.getTime());
        String yueRi = TimeUtils.timeFatmat(datas.get(position).time,1);

        vh.item_sp_date_tv.setText(yueRi);//月-日


        vh.item_sp_carili_tv.setText(NumUtils.disIntZero((int) data.caloric,N2));

        float d=NumUtils.numBaoLiuWei2(data.distances);
        vh.item_sp_distance_tv.setText(NumUtils.floatDis2XSH_2(d,N2));

        vh.item_sp_xl_tv.setText(NumUtils.disIntZero(data.heartCount,N2));

        vh.item_sp_shm_tv.setText(BRA_SHM_Fragment.min2Hmin((int) data.sleepTime));

        vh.item_sp_bushu_tv.setText(NumUtils.disIntZero(data.totalSteps,N2));


        adapterButtomLineView(vh.buttomLine,position,yueRi);




        return convertView;
    }

    public String disFlZero(float value,String replaceZero){
        if (value==0){
            return replaceZero;
        }
        return value+"";
    }


    /**
     *
     * @param buttomLine 底部横线控件
     * @param position 当前位置position
     * @param currentYueRi 当前月-日字符串
     */
    private void adapterButtomLineView(View buttomLine, int position, String currentYueRi) {

        try {
            ViewUtils.displayView(buttomLine);
            String nextYueRi = TimeUtils.timeFatmat(datas.get(position+1).time,1);
            if (TextUtils.equals(currentYueRi,nextYueRi)){
                //当前的月日日期跟下一个的月日日期是一样的 底部横线gone
                ViewUtils.goneView(buttomLine);
            }
        }catch (Exception e){
            e.printStackTrace();
        }


    }


    static class VH{
        View topLine;
        View buttomLine;

        TextView item_sp_date_tv;
        TextView item_sp_bushu_tv;
        TextView item_sp_distance_tv;
        TextView item_sp_carili_tv;
        TextView item_sp_xl_tv;
        TextView item_sp_shm_tv;
     }
}
